﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace AgvPublic
{
    public class PublicFunction
    {
        /// <summary>
        /// 十六进制换算为十进制
        /// </summary>
        /// <param name="strColorValue"></param>
        /// <returns></returns>
        public static int GetHexadecimalValue(String strColorValue)
        {
            char[] nums = strColorValue.ToCharArray();
            int total = 0;
            try
            {
                for (int i = 0; i < nums.Length; i++)
                {
                    String strNum = nums[i].ToString().ToUpper();
                    switch (strNum)
                    {
                        case "A":
                            strNum = "10";
                            break;
                        case "B":
                            strNum = "11";
                            break;
                        case "C":
                            strNum = "12";
                            break;
                        case "D":
                            strNum = "13";
                            break;
                        case "E":
                            strNum = "14";
                            break;
                        case "F":
                            strNum = "15";
                            break;
                        default:
                            break;
                    }
                    double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1));
                    total += Convert.ToInt32(strNum) * Convert.ToInt32(power);
                }
            }
            catch (System.Exception ex)
            {
                String strErorr = ex.ToString();
                return 0;
            }
            return total;
        }


        /// <summary>
        /// 根据发送实体构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetSendData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[3] = 0x0F;

            #region 根据传入参数构造命令码

            switch (model.CmdCode)
            {
                case 1://启动AGV
                    {
                        SendMsg[4] = 0x01;
                        SendMsg[5] = BitConverter.GetBytes(model.Direction)[0];
                        SendMsg[6] = BitConverter.GetBytes(model.TargetStation)[0];
                        break;
                    }
                case 2://停止AGV
                    {
                        SendMsg[4] = 0x02;
                        SendMsg[5] = 0x00;
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 3://复位AGV
                    {
                        SendMsg[4] = 0x03;
                        SendMsg[5] = 0x00;
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 4://调度AGV
                    {
                        SendMsg[4] = 0x04;
                        SendMsg[5] = BitConverter.GetBytes(model.PanType)[0];
                        SendMsg[6] = BitConverter.GetBytes(model.Direction)[0];
                        SendMsg[7] = BitConverter.GetBytes(model.TargetStation)[0];
                        break;
                    }
                case 5://充电命令
                    {
                        SendMsg[4] = 0x05;
                        SendMsg[5] = BitConverter.GetBytes(model.Direction)[0];
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 6://锁定
                    {
                        SendMsg[4] = 0x06;
                        SendMsg[5] = 0x00;
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 7://解锁
                    {
                        SendMsg[4] = 0x07;
                        SendMsg[5] = 0x00;
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 8://清空
                    {
                        SendMsg[4] = 0x08;
                        SendMsg[5] = 0x00;
                        SendMsg[6] = 0x00;
                        break;
                    }
                case 9://清空
                    {
                        SendMsg[4] = 0x79;
                        SendMsg[5] = BitConverter.GetBytes(model.Direction)[0];
                        SendMsg[6] = 0x00;
                        break;
                    }
            }

            if (model.CmdCode != 4)
            {
                for (int i = 0; i < 7; i++)
                {
                    SendMsg[7 + i] = 0x00;
                }
            }
            else
            {
                for (int i = 0; i < 6; i++)
                {
                    SendMsg[8 + i] = 0x00;
                }
            }
            SendMsg[14] = 0x20;
            SendMsg[15] = 0xFF;

            #endregion

            return SendMsg;
        }



        public static byte[] GetBaoJing(SendModel model)
        {
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[3] = 0x0F;
            SendMsg[4] = 0x08;
            for (int i = 0; i < 8; i++)
            {
                 SendMsg[5 + i] = 0x00;
            }
            SendMsg[13] = 0x01;
            SendMsg[14] = 0x20;
            SendMsg[15] = 0xFF;

            return SendMsg;
        }


        /// <summary>
        /// 根据发送调度实体构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetDispatchData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            switch (model.CmdCode)
            {
                case 1://启动AGV
                    {
                        SendMsg[4] = 0x72;
                     
                        break;
                    }
                case 2://放行AGV
                    {
                        SendMsg[4] = 0x77;
                       
                        break;
                    }
            }
            
            SendMsg[5] = BitConverter.GetBytes(model.TargetStation)[0];
            SendMsg[6] = 0x01;
            SendMsg[7] = 0x04;
            SendMsg[8] = BitConverter.GetBytes(model.PanType)[0];
            for (int i = 0; i < 5; i++)
            {
                SendMsg[9 + i] = 0x00;
            }
            SendMsg[14] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[15] = 0xFF;



            return SendMsg;
        }

        /// <summary>
        /// 根据发送调度实体构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetBackDispatchData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            switch (model.CmdCode)
            {
                case 1://启动AGV
                    {
                        SendMsg[4] = 0x52;

                        break;
                    }
                case 2://放行AGV
                    {
                        SendMsg[4] = 0x51;

                        break;
                    }
                case 3://空料AGV
                    {
                        SendMsg[4] = 0x58;

                        break;
                    }
            }

            SendMsg[5] = BitConverter.GetBytes(model.TargetStation)[0];
            SendMsg[6] = 0x01;
            SendMsg[7] = 0x04;
            SendMsg[8] = BitConverter.GetBytes(model.PanType)[0];
            for (int i = 0; i < 5; i++)
            {
                SendMsg[9 + i] = 0x00;
            }
            SendMsg[14] = BitConverter.GetBytes(model.TargetStation)[0];
            SendMsg[15] = 0xFF;



            return SendMsg;
        }

        /// <summary>
        /// 充电桩
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static byte[] GetFloorChargeData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;           
            SendMsg[4] = 0x55;             
            switch (model.CmdCode)
            {
                case 1://左侧充电
                    {
                        SendMsg[5] = 0x01;

                        break;
                    }
                case 2://右侧充电
                    {
                        SendMsg[5] = 0x02;

                        break;
                    }
                case 3://充电停止
                    {
                        SendMsg[5] = 0x03;

                        break;
                    }
            }            
            for (int i = 0; i < 8; i++)
            {
                SendMsg[6 + i] = 0x00;
            }
            SendMsg[14] = 0x10;
            SendMsg[15] = 0xFF;
            return SendMsg;
        }

        /// <summary>
        /// 红绿灯
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static byte[] GetLampData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            SendMsg[4] = 0x54;
            switch (model.CmdCode)
            {
                case 1://绿灯
                    {
                        SendMsg[5] = 0x01;

                        break;
                    }
                case 2://红灯
                    {
                        SendMsg[5] = 0x02;

                        break;
                    }             
            }
            for (int i = 0; i < 8; i++)
            {
                SendMsg[6 + i] = 0x00;
            }
            SendMsg[14] = 0x10;
            SendMsg[15] = 0xFF;
            return SendMsg;
        }

        /// <summary>
        /// 串口通信数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static byte[] GetSerialData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[10];
            SendMsg[0] = 0xFD;
            SendMsg[1] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[2] = 0x00;
            SendMsg[3] = 0x00;
            SendMsg[4] = 0x00;
            SendMsg[5] = 0x00;
            SendMsg[6] = 0x00;
            SendMsg[7] = 0x00;
            SendMsg[8] = 0x00;
            SendMsg[9] = 0xFF;
            return SendMsg;
        }

        /// <summary>
        /// 根据发送电梯升降构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetMesData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            SendMsg[4] = 0x70;

            SendMsg[5] = BitConverter.GetBytes(model.TargetStation)[0];//1车目标
            SendMsg[6] = BitConverter.GetBytes(model.ActionType)[0];//1车当前
            SendMsg[7] = BitConverter.GetBytes(model.CmdCode)[0];//2车目标
            SendMsg[8] = BitConverter.GetBytes(model.Direction)[0];//2车当前

            for (int i = 0; i < 4; i++)
            {
                SendMsg[9 + i] = 0x00;
            }
            SendMsg[13] = 0x01;
            SendMsg[14] = 0x02;
            SendMsg[15] = 0xFF;



            return SendMsg;
        }

        /// <summary>
        /// 根据MES发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetElevatorData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            SendMsg[4] = 0x43;
            switch (model.CmdCode)
            {
                case 1://启动AGV
                    {
                        SendMsg[5] = 0x01;

                        break;
                    }
                case 2://放行AGV
                    {
                        SendMsg[5] = 0x02;

                        break;
                    }
            }


            for (int i = 0; i < 8; i++)
            {
                SendMsg[6 + i] = 0x00;
            }
            SendMsg[14] = 0x10;
            SendMsg[15] = 0xFF;



            return SendMsg;
        }


        /// <summary>
        /// 根据发送充电桩构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetChargeData(int cmd)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;
            switch (cmd)
            {
                case 1://启动AGV
                    {
                        SendMsg[4] = 0x21;

                        break;
                    }
                case 2://放行AGV
                    {
                        SendMsg[4] = 0x22;

                        break;
                    }
            }

            for (int i = 0; i < 9; i++)
            {
                SendMsg[5 + i] = 0x00;
            }
            SendMsg[14] = 0x20;
            SendMsg[15] = 0xFF;



            return SendMsg;
        }

        /// <summary>
        /// 根据发送实体构造发送命令码
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetUpDownData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[3] = 0x0F;
            SendMsg[4] = 0x79;
            SendMsg[5] = BitConverter.GetBytes(model.TargetStation)[0];
            #region 根据传入参数构造命令码

            switch (model.CmdCode)
            {
                case 1://上升
                    {

                        SendMsg[6] = 0x03;
                        break;
                    }
                case 2://下降
                    {
                       
                        SendMsg[6] = 0x04;
                        break;
                    }
                case 3://取反
                    {

                        SendMsg[6] = 0x02;
                        break;
                    }
            }
            for (int i = 0; i < 7; i++)
            {
                SendMsg[7 + i] = 0x00;
            }
            SendMsg[14] = BitConverter.GetBytes(model.AgvNum)[0];
            SendMsg[15] = 0xFF;

            #endregion

            return SendMsg;
        }


        public static MemoryStream CreateCodeImg(string checkCode)
        {
            if (string.IsNullOrEmpty(checkCode))
            {
                return null;
            }
            Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
            Graphics graphic = Graphics.FromImage(image);
            try
            {
                Random random = new Random();
                graphic.Clear(Color.White);
                int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
                for (int index = 0; index < 25; index++)
                {
                    x1 = random.Next(image.Width);
                    x2 = random.Next(image.Width);
                    y1 = random.Next(image.Height);
                    y2 = random.Next(image.Height);

                    graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }
                Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
                graphic.DrawString(checkCode, font, brush, 2, 2);

                int x = 0;
                int y = 0;

                //画图片的前景噪音点
                for (int i = 0; i < 100; i++)
                {
                    x = random.Next(image.Width);

                    y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }
                //画图片的边框线
                graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                //将图片验证码保存为流Stream返回
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms;
            }
            finally
            {
                graphic.Dispose();
                image.Dispose();
            }
        }

        /// <summary>
        /// 生成验证码字符串
        /// </summary>
        /// <param name="codeLen">验证码字符长度</param>
        /// <returns>返回验证码字符串</returns>
        public static string MakeCode(int codeLen)
        {
            if (codeLen < 1)
            {
                return string.Empty;
            }
            int number;
            StringBuilder sbCheckCode = new StringBuilder();
            Random random = new Random();

            for (int index = 0; index < codeLen; index++)
            {
                number = random.Next();

                if (number % 2 == 0)
                {
                    sbCheckCode.Append((char)('0' + (char)(number % 10))); //生成数字
                }
                else
                {
                    sbCheckCode.Append((char)('A' + (char)(number % 26))); //生成字母
                }
            }
            return sbCheckCode.ToString();
        }


        /// <summary>
        /// 长春放行任务
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetBackData(SendModel model)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFD;
            SendMsg[1] = 0x10;
            SendMsg[2] = 0x20;
            SendMsg[3] = 0x0F;            
            SendMsg[4] = 0x57;              
            SendMsg[5] = BitConverter.GetBytes(model.AgvNum)[0];          
            for (int i = 0; i < 8; i++)
            {
                SendMsg[6 + i] = 0x00;
            }
            SendMsg[14] = BitConverter.GetBytes(model.TargetStation)[0];
            SendMsg[15] = 0xFF;
            return SendMsg;
        }

        /// <summary>
        /// 长沙调度拼接
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        //public static byte[] GetSendDispatchData(Model.Agv_Dispatch_Command command,int id)
        //{
        //    //构建头文件
        //    byte[] SendMsg = new byte[16];
        //    SendMsg[0] = 0xFA;
        //    SendMsg[1] = BitConverter.GetBytes(id)[0];
        //    SendMsg[2] = BitConverter.GetBytes(command.Agv_Num)[0];
        //    SendMsg[3] = BitConverter.GetBytes(command.Road_Num)[0];
        //    SendMsg[4] = BitConverter.GetBytes(command.Station_Num)[0];
        //    SendMsg[5] = BitConverter.GetBytes(command.Park_Model)[0];
        //    SendMsg[6] = BitConverter.GetBytes(command.Auto_Start_Direction)[0];
        //    SendMsg[7] = BitConverter.GetBytes(command.Start_Time)[0];
        //    SendMsg[8] = BitConverter.GetBytes(command.Action_Cmd)[0];
        //    SendMsg[9] = BitConverter.GetBytes(command.Speed_Cmd)[0];
        //    SendMsg[10] = BitConverter.GetBytes(command.Fork_Cmd)[0];
        //    SendMsg[11] = BitConverter.GetBytes(command.Obstacle_Cmd)[0];
        //    SendMsg[12] = BitConverter.GetBytes(command.Jump_Cmd)[0];
        //    SendMsg[13] = BitConverter.GetBytes(command.Start_Direction)[0];
        //    int tempNum = id + command.Agv_Num + command.Road_Num + command.Station_Num + command.Park_Model + command.Auto_Start_Direction + command.Start_Time
        //        + command.Action_Cmd + command.Speed_Cmd + command.Fork_Cmd + command.Obstacle_Cmd + command.Jump_Cmd + command.Start_Direction;
        //    int num = tempNum % 256;
        //    SendMsg[14] = BitConverter.GetBytes(num)[0];
        //    SendMsg[15] = 0xFB;
        //    return SendMsg;
        //}

        /// <summary>
        /// 调度回复
        /// </summary>
        /// <param name="model">发送实体</param>
        /// <returns></returns>
        public static byte[] GetBackDispatchChangData(int agvNum,int id)
        {
            //构建头文件
            byte[] SendMsg = new byte[16];
            SendMsg[0] = 0xFA;
            SendMsg[1] = 0xF0;
            SendMsg[2] = BitConverter.GetBytes(agvNum)[0];
            SendMsg[3] = BitConverter.GetBytes(id)[0];
            //SendMsg[4] = 0x73;
            for (int i = 0; i < 10; i++)
            {
                SendMsg[4 + i] = 0x00;
            }
            int tempNum = 240 + agvNum + id;
            int num = tempNum % 256;
            SendMsg[14] = BitConverter.GetBytes(num)[0];
            SendMsg[15] = 0xFB;
            return SendMsg;
        }


        public static List<string> GetSendPlcData(int sendCode)
        {
            List<string> tempList = new List<string>();
            for (int i = sendCode; i < sendCode+9; i++)
            {
                if (i == sendCode)
                {
                    string sendStringM = "RD MR" + i + "\r";
                    string sendStringD = "RD DM" + i + "\r";
                    tempList.Add(sendStringM);
                    tempList.Add(sendStringD);
                }
                else if (i == sendCode + 2)
                {
                    string sendStringM = "RD MR" + i + "\r";
                    string sendStringD = "RD DM" + i + "\r";
                    tempList.Add(sendStringM);
                    tempList.Add(sendStringD);
                }
                else
                {
                    string sendString = "RD MR" + i + "\r";
                    tempList.Add(sendString);
                }
                
            }
            return tempList;
        }

        public static List<string> GetAllPlcData(int sendCode)
        {
            List<string> tempList = new List<string>();
            for (int i = sendCode; i < sendCode + 9; i++)
            {
                if (i == sendCode)
                {
                    string sendStringM = "RD MR" + i + "\r";
                    string sendStringD = "RD DM" + i + "\r";
                    tempList.Add(sendStringM);
                    tempList.Add(sendStringD);
                }
                else if (i == sendCode + 2)
                {
                    string sendStringM = "RD MR" + i + "\r";
                    string sendStringD = "RD DM" + i + "\r";
                    tempList.Add(sendStringM);
                    tempList.Add(sendStringD);
                }
                else
                {
                    string sendString = "RD MR" + i + "\r";
                    tempList.Add(sendString);
                }

            }
            return tempList;
        }
    }
}
